#!/bin/bash

############################################################################
#
# 远程执行该脚本，请在命令行中直接执行以下命令：
#
#   curl -sSL https://gitee.com/tay3223/biubiubiu/raw/master/centos7-proxy-linux.sh | /bin/bash
#
#
############################################################################



cat <<\EOF > /etc/profile.d/proxy.sh

#代理地址（全局只需修改这里即可）
proxy_address=http://192.168.5.242:7890

#代理忽略地址（全局只需修改这里即可）
no_proxy_address=localhost,127.0.0.0/8,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,.kubernetes,.default,.default.svc,.svc,.default.svc.cluster.local,.svc.cluster.local,.cluster.local,.tianye.work,.taycc.work,.aliyun.com
#
# k8s内部会跟以下域名内部通信，需要加入到忽略名单中
# kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local
#


##########################
# 开启系统代理
##########################
function proxy_on() {
    export http_proxy=$proxy_address
    export https_proxy=$proxy_address
    export no_proxy=$no_proxy_address

    export HTTP_PROXY=$proxy_address
    export HTTPS_PROXY=$proxy_address
    export NO_PROXY=$no_proxy_address
    echo -e "\033[32m[√] 已开启全局代理\033[0m"
}

##########################
# 关闭系统代理
##########################
function proxy_off(){
    unset http_proxy
    unset https_proxy
    unset no_proxy

    unset HTTP_PROXY
    unset HTTPS_PROXY
    unset NO_PROXY
    echo -e "\033[31m[×] 已关闭全局代理\033[0m"
}

##########################
# 开启docker专属代理
##########################
function proxy_docker_on(){
    #关闭全局代理
    proxy_off

    mkdir -p /etc/systemd/system/docker.service.d

#注意：此处的EOF应该把变量转换为value，不要使用反斜杠
cat << Tay > /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=$proxy_address"
Environment="HTTPS_PROXY=$proxy_address"
Environment="NO_PROXY=$no_proxy_address"
Tay

    systemctl daemon-reload && systemctl restart docker
    echo -e "\033[32m[√] 已开启docker代理\033[0m"

}

##########################
# 关闭docker专属代理
##########################
function proxy_docker_off(){
    mkdir -p /etc/systemd/system/docker.service.d
    rm -rf /etc/systemd/system/docker.service.d/http-proxy.conf
    systemctl daemon-reload && systemctl restart docker
    echo -e "\033[31m[×] 已关闭docker代理\033[0m"
}

##########################
# 开启containerd专属代理
##########################
function proxy_containerd_on(){
    #关闭全局代理
    proxy_off

    mkdir -p /etc/systemd/system/containerd.service.d

#注意：此处的EOF应该把变量转换为value，不要使用反斜杠
cat << Tay > /etc/systemd/system/containerd.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=$proxy_address"
Environment="HTTPS_PROXY=$proxy_address"
Environment="NO_PROXY=$no_proxy_address"
Tay

    systemctl daemon-reload && systemctl restart containerd
    echo -e "\033[32m[√] 已开启containerd代理\033[0m"

}

##########################
# 关闭containerd专属代理
##########################
function proxy_containerd_off(){
    mkdir -p /etc/systemd/system/containerd.service.d
    rm -rf /etc/systemd/system/containerd.service.d/http-proxy.conf
    systemctl daemon-reload && systemctl restart containerd
    echo -e "\033[31m[×] 已关闭containerd代理\033[0m"
}
EOF




cat << Tay

====================================

打开 /etc/profile.d/proxy.sh 文件，修改<proxy_address>等相关变量的值即可调整参数

重新加载环境变量: source /etc/profile

-------------------

开启系统代理: proxy_on

关闭系统代理: proxy_off

-------------------

开启docker代理: proxy_docker_on

关闭docker代理: proxy_docker_off

-------------------

开启docker代理: proxy_containerd_on

关闭docker代理: proxy_containerd_off
====================================

Tay
